const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter

const func = require('./func.js')


// 页面初始加载数据
const page_init=(that, user_id, jg_id)=>{
  wx.showLoading({title: '页面初始化……'})
  wx.request({
    url: domainName + '/register_index_init',
    data: {
      jg_id,
      user_id,
    },...parameter,
    success: res => {
      console.log('res',res.data)
      that.setData({
        show_inputPic:res.data.show_inputPic,
      })


      let stu_info = res.data.stu_info
      let status_list = res.data.status_list

        // 1、状态为【1，2，6，8，11】（申请中、被拒绝、已毕业、被退学、被删除），以及无状态用户,可进入该页面
        // 2、仅只有状态为【1，2】的学员可以在原行中，更改信息（可显示stu_info数据）；其他的状态都应该重新创建行
      if([3,4,5,7,9,10].indexOf(stu_info.status)>-1){
        // 【3，4，5，6，9，10】（在训、申请考试、已报考、暂停训练、初考、补考）
        wx.showModal({
          content: '您当前是【' + func.idToName(status_list,stu_info.status) + '】学员，不可填写此申请。\n若有疑问，请联系客服。',
          showCancel: false,
          title: '提示',
          success: (result) => {wx.switchTab({url: '/pages/index/index'})
          },
        })
      }else{
        if([1,2].indexOf(stu_info.status)>-1){
          that.jg_stu_id = stu_info.jg_stu_id
          that.setData({
            stu_info:stu_info,
          })
        }
        that.setData({
          status_list:status_list,
          
          user_info:res.data.user_info,
          jg_info:res.data.jg_info,
        })
      }
      wx.hideLoading()
    },
  })
}

  // 用户登录
const userLogin_pageInit=(that,jg_id)=>{
    wx.login({
      success: res => {
        wx.request({
          url: domainName + '/register_index_getOpenid',
          data: {
            js_code:res.code
          },...parameter,
          success: res => {
            let user_id = res.data.user_id
            that.user_id = user_id
            wx.setStorageSync('user_id', user_id)
            page_init(that, user_id, jg_id)
          },
        })
      }
    })
  }

  // 仅上传文字数据
const upload_text=(that, submit_data)=>{
  wx.showLoading({
    title: '提交文本……',
  })
  wx.request({
    url: domainName + '/register_index_uploadText',
    data: {
      submit_data:JSON.stringify(submit_data),
    },...parameter,
    success: res => {
      wx.hideLoading()
      // console.log('res',res.data)
      if(res.data.update_res || res.data.update_res == 0){
        wx.showModal({
          content:'请等待教务审核',
          cancelColor: '#999999',
          cancelText: '完成',
          confirmColor: '#00b050',
          confirmText: '前往做题',
          title: '提交完成',
          success: res => {
            if(res.confirm){
              wx.switchTab({
                url: '/pages/index/index',
              })
            }else{
              that.onLoad(that.options)
            }
          },
        })
      }else{
        wx.showModal({
          content: '更新失败，请联系客服',
          title: '提示',
          success: res => {
            if(res.confirm){
              wx.navigateTo({
                url: '/pages/contact_admin/contact_admin',
              })
            }
          },
        })
      }
    }
  })
}

// 上传图片+文字信息
const upload_text_photo=(that,submit_data)=>{
  wx.showLoading({
    title: '提交图片及文本……',
  })
  wx.uploadFile({
    filePath: that.data.tempFilePath,
    name: 'files',
    url: domainName + 'register_index_uploadTextPhoto',
    formData: {
      submit_data:JSON.stringify(submit_data || {}),
    },
    header : {'content-type':'application/x-www-form-urlencoded'},
    success: res => {
      wx.hideLoading()
      // console.log('res',res.data)
      if(JSON.parse(res.data).row_id){
        wx.showModal({
          content:'请等待审核',
          cancelColor: '#999999',
          cancelText: '完成',
          confirmColor: '#00b050',
          confirmText: '前往做题',
          title: '提交完成',
          success: res => {
            if(res.confirm){
              wx.switchTab({
                url: '/pages/index/index',
              })
            }else{
              page_init(that, submit_data.user_id, submit_data.jg_id)
            }
          },
        })
      }else{
        wx.showModal({
          content: '提交失败，请联系客服',
          title: '提示',
          success: res => {
            if(res.confirm){
              wx.navigateTo({
                url: '/pages/contact_admin/contact_admin',
              })
            }
          },
        })
      }
    },
  })
}


// 加载该用户个人信息
const query_basicInfo=(that,user_id)=>{
  wx.showLoading({title: '页面初始化……'})
  wx.request({
    url: domainName + '/load_basicInfo',
    data: {
      user_id,
    },...parameter,
    success: res => {
      that.setData({
        user_info:res.data.user_info,
      })
      wx.hideLoading()
    },
  })
}

// 获取用户手机号
const getphonenumber=(that,user_id,phone_code)=>{
  wx.showLoading({
    title: '正在授权...',
  })
  // console.log('获取用户手机号',e)
  // 前往服务器，获取APP_SECRET
  wx.request({
    url: domainName + '/register_index_getPhoneNumber',
    data: {
      user_id,
      phone_code,
    },...parameter,
    success: res => {
      wx.hideLoading()
      var update_res = res.data.update_res
      if(update_res){
        wx.showToast({
          title: '授权成功',
          duration: 1500,
          icon: 'success',
          mask: false,
        })
      }else{
        wx.showModal({
          content: '手机验证失败，稍后重试',
          showCancel: false,
          title: '授权失败',
        })
      }
      let user_info = that.data.user_info
      user_info.phone = res.data.phone
      that.setData({
        user_info:user_info
      })
    }
  })
}

module.exports={
  upload_text_photo,
  getphonenumber,
  query_basicInfo,
  page_init,
  userLogin_pageInit,
  upload_text,
}